package edu.hgnu.consumer;

import com.rabbitmq.client.*;
import edu.hgnu.enums.LogType;
import edu.hgnu.utils.RabbitMQUtils;

import java.io.IOException;

public class Consumer2 {
    public static void main(String[] args) throws Exception{
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "logs_direct";

        channel.exchangeDeclare(exchangeName,"direct");
        String queue = channel.queueDeclare().getQueue();

        //临时队列和交换机绑定
        channel.queueBind(queue,exchangeName, LogType.getValueByIndex(1));
        channel.queueBind(queue,exchangeName, LogType.getValueByIndex(2));
        channel.queueBind(queue,exchangeName, LogType.getValueByIndex(3));
        channel.queueBind(queue,exchangeName, LogType.getValueByIndex(4));

        System.out.println(LogType.getValueByIndex(1)+"==>"+LogType.getTextByIndex(1));
        System.out.println(LogType.getValueByIndex(2)+"==>"+LogType.getTextByIndex(2));
        System.out.println(LogType.getValueByIndex(3)+"==>"+LogType.getTextByIndex(3));
        System.out.println(LogType.getValueByIndex(4)+"==>"+LogType.getTextByIndex(4));

        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            /**
             * @param consumerTag
             * @param envelope
             * @param properties
             * @param body
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2："+new String(body));
            }
        });

    }
}
